package com.example.beikeyigou.login_reg;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.beikeyigou.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class LocalImgAdapter extends BaseAdapter {
    Context context;
    List<String> data;
    MemoryCache cache = new MemoryCache();

    public LocalImgAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_loca_img, parent, false);
            vh = new VH(convertView);
            convertView.setTag(vh);
        } else {
            vh = (VH) convertView.getTag();
        }
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                String path = data.get(position);
//                Bitmap bm = cache.get(path);
//                if (bm == null) {
//                    Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path),
//                            200, 0, true);
//                    cache.put(path, bitmap);
//                    vh.iv.setImageBitmap(bitmap);
//                } else {
//                    vh.iv.setImageBitmap(bm);
//                }
//            }
//        }.start();

//        String path = data.get(position);
//        Bitmap bm = cache.get(path);
//        if (bm == null) {
//            Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path),
//                    200, 0, true);
//            cache.put(path, bitmap);
//            vh.iv.setImageBitmap(bitmap);
//        } else {
//            vh.iv.setImageBitmap(bm);
//        }

        new loadImg(vh, position).execute();
        return convertView;
    }

    private class loadImg extends AsyncTask {
        private VH vh;
        private int position;

        public loadImg(VH vh, int position) {
            this.vh = vh;
            this.position = position;
        }

        @Override
        protected Object doInBackground(Object[] objects) {
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            String path = data.get(position);
            Bitmap bm = cache.get(path);
            if (bm == null) {
                Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path),
                        200, 0, true);
                cache.put(path, bitmap);
                vh.iv.setImageBitmap(bitmap);
            } else {
                vh.iv.setImageBitmap(bm);
            }
        }
    }

    /**
     * bgp_w_picpath: 需要处理的图片
     * newWidth 、newHeight: 这张图片处理后的宽高，这里newWidth 和 newHeight 其有效值是>= 0;
     *      当其中一个值为0时候，表示按等宽* 高比来放大或是缩小。如果两个值都为0，则不进行放大缩小。
     * recy_the_bitmap:其值是一个boolean 值。其作用是：对bgp_w_picpath  这张图片处理完成后是否回收其资源，
     *      对于手机这样的移动设备开发，其资源非常有限，这个是必须的。
     */
    public static Bitmap zoomImage(Bitmap bgp_w_picpath, int newWidth,
                                   int newHeight, boolean recy_the_bitmap) { // 记得函数声明为static
        int width;
        int height;
        Bitmap bitmap_mage = null;

        if(bgp_w_picpath != null && !(newWidth==0 && newHeight==0)) {
            float scaleWidth = 1.0f;
            float scaleHeight = 1.0f;

            width = bgp_w_picpath.getWidth();
            height = bgp_w_picpath.getHeight();
            Matrix matrix = new Matrix();

            if(newWidth > 0) {
                scaleWidth = ((float) newWidth) / width;
            }

            if(newHeight > 0) {
                scaleHeight = ((float) newHeight) / height;
            }

            if(scaleWidth == 1.0f) {
                scaleWidth = scaleHeight;
            }

            if(scaleHeight == 1.0f) {
                scaleHeight = scaleWidth;
            }

            matrix.postScale(scaleWidth, scaleHeight);
            bitmap_mage = Bitmap.createBitmap(bgp_w_picpath, 0, 0, width, height, matrix, true);
        }


        if(bitmap_mage != null) {
            if(recy_the_bitmap) {// 回收资源
                bgp_w_picpath.recycle();
                bgp_w_picpath = null;
            }
            return bitmap_mage;
        }
        else {
            return bgp_w_picpath;
        }

    }

    class VH {
        ImageView iv;
        public VH (View v) {
            iv = v.findViewById(R.id.item_local_img_iv);
        }
    }
}
